package com.mfoyou.agent.web.controller.account;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;

import org.mfoyou.agent.common.dao.domain.HuawuServiceCoupon;
import org.mfoyou.agent.common.dao.domain.HuawuServiceCouponRule;
import org.mfoyou.agent.common.inf.IOther;
import org.mfoyou.agent.utils.common.ArrayListObject;
import org.mfoyou.agent.utils.common.JsonRet;
import org.mfoyou.agent.utils.common.ListObject;
import org.mfoyou.agent.utils.common.MfoyouEncrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mfoyou.agent.web.intercepter.UrlRight;
import com.mfoyou.agent.web.intercepter.UserRight;
import com.mfoyou.agent.web.service.session.SessionService;

/**
 * serviceCouponController
 *
 * @author wb-zh270168
 */
@Path("serviceCoupon")
@Service
public class ServiceCouponController {

    @Autowired
    SessionService sessionService;

    @Autowired
    IOther otherService;

    /**
     * 添加优惠券，
     *
     * @param request
     * @param json
     * @return 结果
     */
    @POST
    @Path(value = "addCoupon")
    @Produces("application/json;charset=UTF-8")
    @UrlRight({UserRight.AGENTACCESS})
    public JsonRet<?> addCoupon(@Context HttpServletRequest request, String json) {
        Integer agentId = sessionService.currentUserId(request);
        HuawuServiceCoupon huawuServiceCoupon = JSON.parseObject(json, HuawuServiceCoupon.class);
        huawuServiceCoupon.setAgentId(agentId);
        int ret = otherService.addServiceCoupon(huawuServiceCoupon);
        return JsonRet.buildRet(ret - 1);
    }

    /**
     * 添加优惠券
     *
     * @param request
     * @param json
     * @return 结果
     */
    @POST
    @Path(value = "modifyCoupon")
    @Produces("application/json;charset=UTF-8")
    @UrlRight({UserRight.AGENTACCESS})
    public JsonRet<?> modifyCoupon(@Context HttpServletRequest request, String json) {
        Integer agentId = sessionService.currentUserId(request);
        HuawuServiceCoupon huawuServiceCoupon = new HuawuServiceCoupon();
        JSONObject parseObject = JSON.parseObject(json);
        Integer couponId = parseObject.getInteger("couponId");
        String couponName = parseObject.getString("couponName");
        Integer couponState = parseObject.getInteger("state");
        if (couponId == null || (couponName == null && couponState == null)) {
            return JsonRet.buildRet(-96);
        }
        HuawuServiceCoupon huawuServiceCouponEx = new HuawuServiceCoupon();
        huawuServiceCouponEx.setAgentId(agentId);
        huawuServiceCouponEx.setServiceCouponId(couponId);
        huawuServiceCoupon.setCouponName(couponName);
        huawuServiceCoupon.setCouponState(couponState);
        int ret = otherService.modifyCoupon(huawuServiceCoupon, huawuServiceCouponEx);
        return JsonRet.buildRet(ret - 1);
    }

    /**
     * 分页查询查询优惠券列表 - huawu_service_coupon && huawu_service_user_coupon
     *
     * @param request
     * @param pageSize
     * @param pageId   页码
     * @param couponId 页大小
     * @param state    优惠券状态: 0-进行中; 1-已结束
     * @return
     */
    @GET
    @Path(value = "getCoupon")
    @Produces("application/json;charset=UTF-8")
    @UrlRight({UserRight.AGENTACCESS})
    public JsonRet<?> getCoupon(@Context HttpServletRequest request, @QueryParam("pageSize") Integer pageSize,
                                @QueryParam("pageId") Integer pageId, @QueryParam("couponId") Integer couponId, @QueryParam("state") Integer state) {
        Integer agentId = 14;//sessionService.currentUserId(request);
        ArrayListObject<?> couponList = otherService.getCouponList(agentId, pageId, pageSize, couponId, state);
        return JsonRet.buildSuccRet(ListObject.BulidListObject(couponList));
    }

    /**
     * 通过服务商id和用户id查询服务商优惠券列表 - huawu_service_coupon && huawu_service_user_coupon
     *
     * @param request
     * @param agentStr
     * @return
     */
    @GET
    @Path(value = "usergetCoupon")
    @Produces("application/json;charset=UTF-8")
    @UrlRight(UserRight.ALLACCESS)
    public JsonRet<?> usergetCoupon(@Context HttpServletRequest request, @QueryParam("agentStr") String agentStr) {
        Integer agentId = MfoyouEncrypt.decode(agentStr);
        if (agentId == null) {
            return JsonRet.buildRet(-96);
        }
        Integer userId = sessionService.currentUserId(request);
        List<HuawuServiceCoupon> list = otherService.getAgentCouponListByUser(agentId, userId);
        return JsonRet.buildSuccRet(list);
    }

    /**
     * 抢优惠券
     *
     * @param request
     * @param couponIds
     * @return
     */
    @GET
    @Path(value = "takeCoupon")
    @Produces("application/json;charset=UTF-8")
    @UrlRight(UserRight.LOGINACCESS)
    public JsonRet<?> takeCoupon(@Context HttpServletRequest request, @QueryParam("couponIds") Integer[] couponIds) {
        if (couponIds == null || couponIds.length == 0) {
            return JsonRet.buildRet(-96);
        }
        Integer userid = sessionService.currentUserId(request);
        List<Integer> ret = otherService.takeCoupons(couponIds, userid);
        return JsonRet.buildSuccRet(ret);
    }

    /**
     * 我的优惠券列表
     *
     * @param request
     * @param state
     * @param type
     * @return
     */
    @GET
    @Path(value = "myCouponList")
    @Produces("application/json;charset=UTF-8")
    @UrlRight(UserRight.LOGINACCESS)
    public JsonRet<?> myCouponList(@Context HttpServletRequest request, @QueryParam("state") Integer state, @QueryParam("type") Integer type) {
        Integer userId = sessionService.currentUserId(request);
        List<Map<String, Object>> coupons = otherService.getUserCouponList(userId, state, type);
        return JsonRet.buildSuccRet(coupons);
    }

    /**
     * 保存 ServiceCoupon 分享优惠券规则
     *
     * @param request
     * @return
     */
    @POST
    @Path(value = "saveServiceCouponRule")
    @Produces("application/json;charset=UTF-8")
    @UrlRight({UserRight.ALLACCESS})
    public JsonRet<?> saveServiceCouponRule(@Context HttpServletRequest request, HuawuServiceCouponRule rule) {
        int result = otherService.saveServiceCouponRule(rule);
        return JsonRet.buildSuccRet(result - 1);
    }

    /**
     * 获取 ServiceCoupon 分享优惠券规则
     *
     * @param request
     * @return
     */
    @GET
    @Path(value = "getServiceCouponRule")
    @Produces("application/json;charset=UTF-8")
    @UrlRight({UserRight.ALLACCESS})
    public JsonRet<?> getServiceCouponRule(@Context HttpServletRequest request) {
        HuawuServiceCouponRule rule = otherService.getServiceCouponRule();
        return JsonRet.buildSuccRet(rule);
    }


}
